#!/bin/bash

source /home/shell/util.sh

read -p "指定数据目录[/home/omnicoredata]：" data_dir
data_dir=${data_dir:-"/home/omnicoredata"};

read -p $'请选择网络类型：\n1. main;\n2. test;\n3. regtest;\n4. signet(暂不支持，还没研究);\n[regtest default]:' net_type
net_type=${net_type:-"3"};


# while [ true ]; do
#     read -p "是否使用精简模式？[y/n 默认n]" prune_mode
#     case $prune_mode in
#             [yY]*)
#                     break
#                     ;;
#             [nN]*)
#                     exit
#                     ;;
#             *)
#                     echo "输入有误，仅支持输入y或n。"
#                     ;;
#     esac
# done




download /opt/omnicore-x86_64-linux-gnu.tar.gz https://hub.fastgit.org/OmniLayer/omnicore/releases/download/v0.11.0/omnicore-0.11.0-x86_64-linux-gnu.tar.gz
tar -xf /opt/omnicore-x86_64-linux-gnu.tar.gz -C  /opt
rm -rf /opt/omnicore-x86_64-linux-gnu.tar.gz
mv /opt/omnicore-*/ /usr/local/omnicore

if [ ! -d "${data_dir}" ]; then
    mkdir -p ${data_dir} 
fi

tee /etc/profile.d/omnicore.sh<<EOF
export PATH=\$PATH:/usr/local/omnicore/bin
EOF
source /etc/profile.d/omnicore.sh

if [ ! -d "~/.bitcoin/" ]; then
    mkdir -p ~/.bitcoin/
fi

tee ~/.bitcoin/bitcoin.conf<<EOF
rpcuser=admin
rpcpassword=admin123
EOF

cp -r /home/shell/support/btc/bitcoin.conf ${data_dir}

case "${net_type}" in
    1)
        printInfo "使用main网！"
        firewall-addport 8332
        sed -i "s/# testnet=1/testnet=0/g" ${data_dir}/bitcoin.conf
    ;;
    2)
        printInfo "使用test网！"
        sed -i "s/# testnet=1/testnet=1/g" ${data_dir}/bitcoin.conf
        firewall-addport 18333
        firewall-addport 18332
    ;;
    3)
        printInfo "使用regtest网！"
        sed -i "s/# regtest=1/regtest=1/g" ${data_dir}/bitcoin.conf
        firewall-addport 18444
    ;;
    4)
        printWarn "不支持signet，即将使用regtest网！"
        sed -i "s/# regtest=1/regtest=1/g" ${data_dir}/bitcoin.conf
    ;;
    *)
        printInfo "使用regtest网！"
        sed -i "s/# regtest=1/regtest=1/g" ${data_dir}/bitcoin.conf
    ;;
esac


# omnicored -conf=${data_dir}/bitcoin.conf -datadir=${data_dir} -daemon
bash /home/shell/serviced-install.sh<<EOF
2
1

omnicored
/usr/local/omnicore/bin/omnicored -conf=${data_dir}/bitcoin.conf -datadir=${data_dir} -daemon




EOF